import {
  Body,
  Controller,
  Get,
  HttpCode,
  Param,
  Post,
  Put,
  Delete,
  Query,
  HttpException,
  HttpStatus,
} from '@nestjs/common';
import { User } from '../entities/user.entity';
import { UserService } from './user.service';

@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Get('hello')
  getHello(): string {
    return this.userService.getHello();
  }
  // 查询所有用户
  @Get()
  async getAll(
    @Query('pageNum') pageNum: number,
    @Query('pageSize') pageSize: number,
  ) {
    try {
      const res = await this.userService.findAll(pageSize, pageNum);
      return {
        code: 200,
        data: {
          users: res,
          count: res.length,
        },
        message: 'sucess',
      };
    } catch (error) {
      console.log(error);
      throw new HttpException(
        {
          status: HttpStatus.FORBIDDEN,
          error: 'This is a custom message',
        },
        HttpStatus.FORBIDDEN,
        {
          cause: error,
        },
      );
    }
  }

  // 创建用户
  @Post()
  @HttpCode(200)
  create(@Body() user: User) {
    // return 'This action adds a new cat';
    return this.userService.create(user);
  }

  // 登录
  @Post('login')
  createLogin(@Body() user: User) {
    try {
      return {
        code: 200,
        data: {
          token: 'token123456789',
        },
      };
    } catch (error) {
      throw new HttpException(
        {
          status: HttpStatus.FORBIDDEN,
          error: 'This is a custom message',
        },
        HttpStatus.FORBIDDEN,
        {
          cause: error,
        },
      );
    }
  }
  // 查询单个用户
  @Post('info')
  findOneInfo(@Query('id') id: string) {
    // return this.userService.findOneInfo(id);
    return {
      code: 200,
      data: {
        roles: [
          {
            name: 'super_admin',
            id: 1,
          },
        ],
      },
    };
  }

  @Get('userone')
  findOne(@Query('id') id: string) {
    return `This action returns a #${id} cat`;
  }

  @Put(':id')
  update(@Param('id') id: string) {
    console.log(id);
    return `This action updates a #${id} cat`;
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return `This action removes a #${id} cat`;
  }
}
